package com.junxonline.plat.controller.role;

import com.junxonline.common.dao.vo.*;
import com.junxonline.common.util.CommonUtils;
import com.junxonline.plat.controller.base.BaseController;
import com.junxonline.plat.dao.vo.*;
import com.junxonline.plat.exception.ErrorCodeEnum;
import com.junxonline.plat.service.role.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author JunX
 * @ClassName: RoleController
 * @Description: 角色控制器
 */

@RestController
@RequestMapping(value = "/admin/role")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    /**
     * 获取角色树数据
     * @return
     */
    @RequestMapping(value = "/getRoleTree")
    public ResultVO getRoleTree() {
        return getReturnSuccess(roleService.getRoleTree());
    }

    /**
     * 获取角色数据
     * @param vo
     * @return
     */
    @RequestMapping(value = "/getRole")
    public ResultVO getRole(@RequestBody QueryRoleVO vo) {

        if (CommonUtils.isEmpty(vo.getRoleId())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        return getReturnSuccess(roleService.getRole(vo));
    }

    /**
     * 筛选查询角色数据
     * @param page
     * @param vo
     * @return
     */
    @RequestMapping(value = "/queryRole")
    public PageResultVO queryRole(PageVO page, QueryRoleVO vo) {
        return getReturnSuccess(roleService.queryRole(page, vo));
    }


    /**
     * 添加角色
     * @param vo
     * @return
     */
    @RequestMapping(value = "/addRole")
    public ResultVO addRole(@RequestBody RoleVO vo) {

        if (CommonUtils.isEmpty(vo) || CommonUtils.isEmpty(vo.getRoleKey()) || CommonUtils.isEmpty(vo.getRoleName()) ||
            CommonUtils.isEmpty(vo.getRoleSort()) || CommonUtils.isEmpty(vo.getRoleStatus())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        boolean success = roleService.addRole(vo);
        if (!success) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();

    }

    /**
     * 修改角色
     * @param vo
     * @return
     */
    @RequestMapping(value = "/editRole")
    public ResultVO editRole(@RequestBody RoleVO vo) {
        if (CommonUtils.isEmpty(vo) || CommonUtils.isEmpty(vo.getRoleId()) || CommonUtils.isEmpty(vo.getRoleName()) ||
                CommonUtils.isEmpty(vo.getRoleKey()) || CommonUtils.isEmpty(vo.getRoleStatus())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        boolean success = roleService.editRole(vo);
        if (!success) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();

    }


    /**
     * 删除角色
     * @param vo
     * @return
     */
    @RequestMapping(value = "/delRole")
    public ResultVO delRole(@RequestBody RoleIdsVO vo) {
        if (CommonUtils.isEmpty(vo)) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        Long[] roleIdsArray = vo.getRoleIds();

        boolean successMenu = roleService.delRole(roleIdsArray);
        if (!successMenu) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();
    }

    /**
     * 设置权限
     * @param vo
     * @return
     */
    @RequestMapping(value = "/setPermission")
    public ResultVO setPermission(@RequestBody SetPermissionVO vo) {
        if (CommonUtils.isEmpty(vo) || CommonUtils.isEmpty(vo.getMenuIds())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        Long[] menuIdsArray = vo.getMenuIds();

        boolean success = roleService.setPermission(vo.getRoleId(), menuIdsArray);
        if (!success) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();
    }

}
